<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 反馈
 */
class Feedback extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiTitle    (反馈)
     * @ApiAuthor   (Zhenzh)
     * @ApiDateTime (2019/8/12 16:46)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/feedback/add)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号", sample="")
     * @ApiParams   (name="note", type="string", required=true, description="反馈内容", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function add()
    {
        $validate = new \think\Validate([
            'mobile' => 'require',
            'note' => 'require',
        ], [], [
            'mobile' => '手机号',
            'note' => '内容',
        ]);

        if (!$validate->check($this->request->request())) {
            $this->error($validate->getError());
        }

        $mobile = $this->request->post('mobile');
        $note = $this->request->post('note');

        if (!\think\Validate::regex($mobile, "^1\d{10}$"))
            $this->error(__('手机号格式不正确'));

        $feedback = new \app\common\model\Feedback;
        $feedback->user_id = $this->auth->id;
        $feedback->mobile = $mobile;
        $feedback->note = $note;
        $feedback->save();

        $this->success('');
    }
}
